﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Problem002
{
    public static class Fibonacci
    {
        private static readonly int startValue = 1;

        private static double? term1;
        private static double? term2;

        public static double NextTerm
        {
            get
            {
                // Series has two our more terms... can proceed by adding them
                if (term1.HasValue && term2.HasValue)
                {
                    term2 += term1;
                    term1 = term2 - term1;
                    return term2.Value;
                }

                // Series hasn't started
                if (!term1.HasValue)
                {
                    term1 = startValue;
                    return startValue;
                }

                // Series has one value only
                if (term1.HasValue && !term2.HasValue)
                {
                    term2 = term1.Value + startValue;
                    return term2.Value;
                }

                throw new InvalidOperationException("One of the if conditions should have been met.");
            }
        }
    }
}
